#include <boost/filesystem.hpp> 
#include <boost/cast.hpp>
#include <string>
//#include <OgreMaterial.h>
//#include <OgreMaterialManager.h>
//#include <OgreRectangle2D.h>
#include <Ogre.h>
#include "Defs.h"
#include "STGLevel.h"
#include "STGEngine.h"
void CSTGLevel::loadConfigScript()
{
    
}

void CSTGLevel::setBackground( const std::string &filename )
{
    if(!mBackground) {
        mBackground = new Ogre::Rectangle2D(true);

    }
    //mBackground->setCorners(-1.0, 1.0, 1.0, -1.0);
    Ogre::MaterialPtr material = (Ogre::MaterialManager::getSingleton().
        createOrRetrieve("background", "Magica")).first;
    
    
    Ogre::TextureUnitState *pTex = material->getTechnique(0)->getPass(0)
        ->createTextureUnitState(filename);
    material->getTechnique(0)->getPass(0)->setDepthCheckEnabled(false);
    material->getTechnique(0)->getPass(0)->setDepthWriteEnabled(false);
    material->getTechnique(0)->getPass(0)->setLightingEnabled(false);

    Ogre::Image im;
    im.load(filename, "Magica");
    float height = boost::numeric_cast<float, size_t>(im.getHeight());
    float width  = boost::numeric_cast<float, size_t>(im.getWidth());
    //float ratio = width / height;
    float width_orig = boost::numeric_cast<float, size_t>(
        CSTGEngine::getSingletonPtr()->getCamera()->
        getViewport()->getActualWidth());
    float height_orig= boost::numeric_cast<float, size_t>(
        CSTGEngine::getSingletonPtr()->getCamera()->
        getViewport()->getActualHeight());
    //float ratio_orig = width_orig / height_orig;
    float ratio = width/height * height_orig/width_orig;
    if (ratio >= 1) // wide screen
        mBackground->setCorners(-1, 1/ratio, 1, -1/ratio);
    else
        mBackground->setCorners(ratio, 1, -ratio, -1);
    mBackground->setMaterial("background");

    mBackground->setRenderQueueGroup(Ogre::RENDER_QUEUE_BACKGROUND);
    mBackground->setBoundingBox(Ogre::AxisAlignedBox(-100000.0*
        Ogre::Vector3::UNIT_SCALE, 100000.0*Ogre::Vector3::UNIT_SCALE));
    if (!mBGNode) {
        mBGNode = static_cast<CSTGEngine*>(CSTGEngine::getSingletonPtr())
            ->getSceneManager()->getRootSceneNode()->
            createChildSceneNode("background");
    }
    mBGNode->attachObject(mBackground);

//pTex->load();
}
